
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @module index
* @author: Hjava
* @description: 新客服模块
* @since: 2018-07-22 19:59:15
*/
var eventEmitter_1 = __webpack_require__(/*! _util/eventEmitter */ "./src/core/utils/eventEmitter.ts");
var kefu_constant_1 = __webpack_require__(/*! _kefu/kefu.constant */ "./src/core/kefu/kefu.constant.ts");
var kefu_session_1 = __webpack_require__(/*! _kefu/kefu.session */ "./src/core/kefu/kefu.session.ts");
var kefu_message_1 = __webpack_require__(/*! _kefu/kefu.message */ "./src/core/kefu/kefu.message.ts");
var analytics_1 = __webpack_require__(/*! _util/analytics */ "./src/core/utils/analytics.ts");
var constant_1 = __webpack_require__(/*! _util/constant */ "./src/core/utils/constant.ts");
var log_1 = __webpack_require__(/*! ./../utils/log */ "./src/core/utils/log.ts");
var KFSDK = /** @class */ (function () {
    function KFSDK(loginSDK) {
        this._loginSDK = null;
        this._timeMap = {};
        this.constant = kefu_constant_1.default;
        this._loginSDK = loginSDK;
        eventEmitter_1.default.on(kefu_constant_1.default.KF.URI_KF_SEND_MSG_REQ, this._onMessage.bind(this));
        eventEmitter_1.default.on(kefu_constant_1.default.KF.URI_KF_SEND_KF_MSG_REQ, this._onMessage.bind(this));
        eventEmitter_1.default.on(kefu_constant_1.default.KF.URI_KF_SEND_MSG_RES, this._onAck.bind(this));
        eventEmitter_1.default.on(kefu_constant_1.default.KF.URI_KF_SEND_KF_MSG_RES, this._onAck.bind(this));
        eventEmitter_1.default.on(kefu_constant_1.default.KF.URI_KF_SYNC_READ_KF, this._onRead.bind(this));
        eventEmitter_1.default.on(kefu_constant_1.default.KF.URI_KF_NOTICE, this._onNotice.bind(this));
    }
    KFSDK.prototype.sendMessage = function (message) {
        var msg = this.selectInnerMessage(message.type, message.body);
        var result = new kefu_session_1.default.KFSendMsgReq({
            fromUid: message.fromUid,
            toUid: message.toUid,
            fromName: message.fromName,
            toAppId: message.toAppId,
            extension: message.extension,
            channel: message.channel,
            message: msg,
            cts: message.cts,
            retries: message.retries
        });
        this.send(result);
        return result;
    };
    KFSDK.prototype._createKFMessage = function (message) {
        var msg = this.selectInnerMessage(message.type, message.body);
        return new kefu_session_1.default.KFSendMsgKFReq({
            senderUid: message.senderUid,
            receiverUid: message.receiverUid,
            receiverAppId: message.receiverAppId,
            bid: message.bid,
            type: message.type,
            message: msg,
            fromName: message.fromName,
            extension: message.extension,
            channel: message.channel,
            msgUuid: message.msgUuid,
            cts: message.cts,
            retries: message.retries
        });
    };
    KFSDK.prototype.sendKFMessage = function (message) {
        var result = this._createKFMessage(message);
        this.send(result);
        analytics_1.default.report({
            nm: 'message_send_start',
            val: {
                id: result.msgUuid,
                chat: result.receiverUid,
                sessionType: 'kfchat'
            }
        });
        this._timeMap[result.msgUuid] = Date.now();
        return result;
    };
    KFSDK.prototype._transFormData = function (message) {
        var quoted = message.quoted || '[]';
        var selected = message.selected || '{}';
        var selectedMessage = message.selectedMessage;
        var add = message.add;
        var quotedMessage = message.quotedMessage;
        if (quoted !== '[]') {
            // 一个引用消息，再被引用
            quoted = JSON.parse(quoted);
            quoted.push({
                // 根据消息类型进行编解码
                message: this._createKFMessage(add.message).getBase64(),
                defaultText: add.defaultText
            });
            quoted = JSON.stringify(quoted);
        }
        else {
            // 一个普通消息被引用
            selected = {
                message: this._createKFMessage(selectedMessage.message).getBase64(),
                defaultText: selectedMessage.defaultText
            };
            selected = JSON.stringify(selected);
            quoted = [{
                    message: this._createKFMessage(add.message).getBase64(),
                    defaultText: add.defaultText
                }];
            quoted = JSON.stringify(quoted);
        }
        return { quoted: quoted, selected: selected, selectedMessage: selectedMessage, quotedMessage: quotedMessage };
    };
    KFSDK.prototype.selectInnerMessage = function (type, body) {
        var msg = null;
        switch (type) {
            case constant_1.MESSAGE_TYPE.MSG_TYPE_TEXT: {
                msg = new kefu_message_1.default.KFTextMessage(body);
                break;
            }
            case constant_1.MESSAGE_TYPE.MSG_TYPE_QUOTE: {
                var res = this._transFormData(body);
                var selected = res.selected;
                var quoted = res.quoted;
                msg = new kefu_message_1.default.KFQuoteMessage({ selected: selected, quoted: quoted });
                break;
            }
            case constant_1.MESSAGE_TYPE.MSG_TYPE_VOICE: {
                msg = new kefu_message_1.default.KFAudioMessage(body);
                break;
            }
            case constant_1.MESSAGE_TYPE.MSG_TYPE_VIDEO: {
                msg = new kefu_message_1.default.KFVideoMessage(body);
                break;
            }
            case constant_1.MESSAGE_TYPE.MSG_TYPE_IMAGE: {
                msg = new kefu_message_1.default.KFImageMessage(body);
                break;
            }
            case constant_1.MESSAGE_TYPE.MSG_TYPE_CALENDAR: {
                msg = new kefu_message_1.default.KFCalendarMessage(body);
                break;
            }
            case constant_1.MESSAGE_TYPE.MSG_TYPE_LINK: {
                msg = new kefu_message_1.default.KFLinkMessage(body);
                break;
            }
            case constant_1.MESSAGE_TYPE.MSG_TYPE_MULTI_LINK: {
                msg = new kefu_message_1.default.KFMultiLinkMessage(body);
                break;
            }
            case constant_1.MESSAGE_TYPE.MSG_TYPE_FILE: {
                msg = new kefu_message_1.default.KFFileMessage(body);
                break;
            }
            case constant_1.MESSAGE_TYPE.MSG_TYPE_GPS: {
                msg = new kefu_message_1.default.KFGPSMessage(body);
                break;
            }
            case constant_1.MESSAGE_TYPE.MSG_TYPE_VCARD: {
                msg = new kefu_message_1.default.KFVCardMessage(body);
                break;
            }
            case constant_1.MESSAGE_TYPE.MSG_TYPE_EMOTION: {
                msg = new kefu_message_1.default.KFEmotionMessage(body);
                break;
            }
            case constant_1.MESSAGE_TYPE.MSG_TYPE_EVENT: {
                msg = new kefu_message_1.default.KFEventMessage(body);
                break;
            }
        }
        return msg;
    };
    KFSDK.prototype.sendRead = function (uid, readItems) {
        var items = readItems.map(function (readItem) {
            return new kefu_session_1.default.KFSyncReadItem(readItem);
        });
        this.send(new kefu_session_1.default.KFSyncRead({
            uid: uid,
            syncReadItems: items
        }));
        analytics_1.default.report({
            nm: 'read_kefu_send',
            val: {
                'web_read': JSON.stringify(readItems.map(function (item) {
                    return {
                        id: item.chatId,
                        peerUid: item.peerUid,
                        peerAppId: item.peerAppId,
                        channel: item.channel,
                        cts: item.stamp
                    };
                }))
            }
        });
    };
    KFSDK.prototype.sendKFRead = function (uid, readItems) {
        var items = readItems.map(function (readItem) {
            return new kefu_session_1.default.KFSyncReadItem(readItem);
        });
        this.send(new kefu_session_1.default.KFSyncReadKF({
            uid: uid,
            syncReadItems: items
        }));
        analytics_1.default.report({
            nm: 'read_kefu_send',
            val: {
                'web_read': JSON.stringify(readItems.map(function (item) {
                    return {
                        id: item.chatId,
                        peerUid: item.peerUid,
                        peerAppId: item.peerAppId,
                        channel: item.channel,
                        cts: item.stamp
                    };
                }))
            }
        });
    };
    KFSDK.prototype.send = function (message) {
        try {
            this._loginSDK._sendTransUp(kefu_constant_1.default.SVID_KEFU, message);
        }
        catch (e) {
            log_1.default.error(e);
        }
    };
    KFSDK.prototype._onMessage = function (message) {
        try {
            this.send(this._ack(message));
        }
        catch (e) {
            log_1.default.error(e);
        }
        eventEmitter_1.default.emit(constant_1.MESSAGE_EVENT.K_MESSAGE, message);
        // logManager.report({
        //     nm: 'message_receive_success',
        //     val: {
        //         id: message.msgUuid,
        //         mid: message.msgId,
        //         chat: userData.get('uid') === message.receiverUid ? message.senderUid : message.receiverUid
        //     }
        // });
        // owlController.send({
        //     name: 'message_receive_success',
        //     value: 0
        // });
    };
    KFSDK.prototype._ack = function (message) {
        return newKFAck(message, this._loginSDK.getUID());
    };
    KFSDK.prototype._onAck = function (message) {
        eventEmitter_1.default.emit(constant_1.MESSAGE_EVENT.K_ACK, message);
        if (message.rescode === 0 && this._timeMap[message.msgUuid]) {
            analytics_1.default.report({
                nm: 'message_send_success',
                val: {
                    id: message.msgUuid,
                    mid: message.msgId,
                    time: Date.now() - this._timeMap[message.msgUuid]
                }
            });
        }
        else {
            analytics_1.default.report({
                nm: 'message_send_error',
                val: {
                    code: message.rescode,
                    id: message.msgUuid
                }
            });
        }
    };
    KFSDK.prototype._onRead = function (syncRead) {
        eventEmitter_1.default.emit(constant_1.MESSAGE_EVENT.K_READ, syncRead);
        analytics_1.default.report({
            nm: 'read_kefu_receive',
            val: {
                'web_read': JSON.stringify(syncRead.syncReadItems.map(function (item) {
                    return {
                        id: item.chatId,
                        peerUid: item.peerUid,
                        peerAppId: item.peerAppId,
                        channel: item.channel,
                        cts: item.stamp
                    };
                }))
            }
        });
    };
    KFSDK.prototype._onNotice = function (notice) {
        eventEmitter_1.default.emit(constant_1.MESSAGE_EVENT.K_NOTICE, notice);
    };
    KFSDK.constant = kefu_constant_1.default;
    KFSDK.Session = kefu_session_1.default;
    KFSDK.Message = kefu_message_1.default;
    return KFSDK;
}());
exports.default = KFSDK;
function newKFAck(message, uid) {
    return new kefu_session_1.default.KFAckMsgs({
        uid: uid,
        msgIds: [message['msgId']]
    });
}


//# sourceURL=webpack://MTDX/./src/core/kefu/index.ts?